<template>
    <div class="content-main">
        <div class="breadcrumb">
            <div class="breadcrumb-icon">
                <img src="./../../assets/images/breadcrumb_icon.png" />
            </div>
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/admin/classMgt' }">班级管理</el-breadcrumb-item>
                <el-breadcrumb-item>添加班级</el-breadcrumb-item>
            </el-breadcrumb>
            <bread-crumb-back></bread-crumb-back>
        </div>
        <div class="content-box">
            <div class="add-class-form">
                <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
                    <el-form-item
                        style="width:40%;"
                        prop="className"
                        label="班级名称"
                        :rules="[
                            { required: true, message: '请输入班级名称', trigger: 'blur' }
                        ]"
                        >
                        <el-input v-model="dynamicValidateForm.className"></el-input>
                    </el-form-item>
                    <div
                        style="width:900px;"
                        v-for="(domain, index) in dynamicValidateForm.domains"
                    >
                        <el-form-item
                            style="width:300px; float:left; margin-right: 20px;"
                            :label="'选择教师'"
                            :prop="'domains.' + index + '.teacherValue'"
                            :rules="{
                                required: true, message: '请选择教师', trigger: 'blur'
                            }"
                            >
                            <el-select
                                v-model="domain.teacherValue"
                                placeholder="请选择教师"
                                value-key="value"
                                style="float:left;">
                                <el-option
                                v-for="item in domain.teacherStatus"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item
                            style="width:auto; float:left;"
                            :label="'选择案例'"
                            :prop="'domains.' + index + '.projectValue'"
                            :rules="{
                                required: true, message: '请选择案例', trigger: 'blur'
                            }"
                            >
                            <el-cascader-multi  style="width:310px;" v-model="domain.projectValue" :show-leaf-label="true" collapse-tags placeholder="请选择案例" :data="domain.projectStatus"> </el-cascader-multi>
                            <!-- <el-select
                                v-model="domain.projectValue"
                                placeholder="请选择案例"
                                multiple
                                collapse-tags
                                value-key="value"
                                style="float:left;">
                                <el-option
                                v-for="item in domain.projectStatus"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option> -->
                            </el-select>
                        </el-form-item>

                        <el-button style="float:left; margin-left:20px;" @click.prevent="removeDomain(domain)">删除</el-button>
                    </div>
                    <el-form-item style="float:left;">
                        <el-button type="primary" @click="submitForm('dynamicValidateForm')">保存</el-button>
                        <el-button @click="addDomain">新增案例</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import BreadCrumbBack from '@/components/breadCrumbBack.vue'
    export default {
        components: {
            BreadCrumbBack
        },
        data () {
            return {
                dynamicValidateForm: {
                    domains: [
                        {
                            teacherStatus: [],
                            teacherValue: '',
                            projectStatus: [],
                            projectValue: []

                        }
                    ],
                    className: ''
                },
                //单独保存下拉数据
                teacherStatus: [],
                projectStatus: []
            }
        },
        created () {
            this.init()
            this.initSelect()
        },
        methods: {
            initSelect(){
                let self = this
                self.$http.get('/dataplatform/major/tree').then((response) => {
                    let res = response.data
                    if(res.code == '200') {
                        for(let i = 0; i < res.data.length; i++){
                            let selectJson = {}
                            selectJson.label = res.data[i].name
                            selectJson.value = res.data[i].id
                            selectJson.children = []
                            if(res.data[i].itemList){
                                for(let j = 0; j < res.data[i].itemList.length; j++){
                                    let selectJson2 = {}
                                    selectJson2.label = res.data[i].itemList[j].name
                                    selectJson2.value = res.data[i].itemList[j].id
                                    selectJson.children.push(selectJson2)
                                }
                            }

                            self.dynamicValidateForm.domains[0].projectStatus.push(selectJson)
                            self.projectStatus.push(selectJson)
                        }
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            init() {
                let self = this
                self.$http.post('/dataplatform/system/class/getAllTeachers').then((response) => {
                    let res = response.data
                    if(res.code == '200') {
                        for(let i = 0; i < res.data.length; i++){
                            let selectJson = {}
                            selectJson.label = res.data[i].userName
                            selectJson.value = res.data[i].id
                            self.dynamicValidateForm.domains[0].teacherStatus.push(selectJson)
                            self.teacherStatus.push(selectJson)
                        }
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
                // self.$http.post('/dataplatform/system/class/getReleaseProjectList?type=1').then((response) => {
                //     let res = response.data
                //     if(res.code == '200') {
                //         for(let i = 0; i < res.data.length; i++){
                //             let selectJson = {}
                //             selectJson.label = res.data[i].name
                //             selectJson.value = res.data[i].id
                //             self.dynamicValidateForm.domains[0].projectStatus.push(selectJson)
                //             self.projectStatus.push(selectJson)
                //         }
                //     }else if(res.code == '401'){
                //         self.$router.replace({
                //             path:'/login'
                //         })
                //     }else{
                //         self.$message.error(res.message)
                //     }
                // })
            },
            submitForm(formName) {
                let self = this
                self.$refs[formName].validate((valid) => {
                    if (valid) {
                        let relationList = []
                        for(let i = 0; i < self.dynamicValidateForm.domains.length; i++){
                            let selectJson = {}
                            selectJson.projectIdList = []
                            for (let j = 0; j < self.dynamicValidateForm.domains[i].projectValue.length; j++) {
                                selectJson.projectIdList.push(self.dynamicValidateForm.domains[i].projectValue[j][1])
                            }
                            // selectJson.projectIdList = self.dynamicValidateForm.domains[i].projectValue
                            selectJson.userId = self.dynamicValidateForm.domains[i].teacherValue
                            relationList.push(selectJson)
                        }
                        self.$http.post('/dataplatform/system/class/addClass',{
                            className: $.trim(self.dynamicValidateForm.className),
                            relationList: relationList
                        }).then((response) => {
                            let res = response.data
                            if(res.code == '200') {
                                self.$message({
                                    message: '保存成功',
                                    type: 'success'
                                })
                                self.$router.push({
                                    path:'/admin/classMgt'
                                })
                            }else if(res.code == '401'){
                                self.$router.replace({
                                    path:'/login'
                                })
                            }else{
                                self.$message.error(res.message)
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                })
            },
            removeDomain(item) {
                var index = this.dynamicValidateForm.domains.indexOf(item)
                if (index !== -1) {
                this.dynamicValidateForm.domains.splice(index, 1)
                }
            },
            addDomain() {
                let self = this
                var domainArr = {
                    teacherStatus: self.teacherStatus,
                    teacherValue: '',
                    projectStatus: self.projectStatus,
                    projectValue: []

                }
                self.dynamicValidateForm.domains.push(domainArr);
            }
        }
    }
</script>
<style>
.el-select-dropdown.el-popper.is-multiple.multi-cascader-style .li-style .li-label-style{
    line-height :34px;
}
</style>
